/**
 * 
 * 医保、自费确认弹框
 */

import React, { Component } from 'react';
import { Form, Modal, Table, Checkbox } from 'antd';
import { Util } from 'tools/index';
import '../style/index.less';

class ShowConfirmModal extends Component {

    constructor(props) {
        super(props);
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        this.state = {
            code: [],
            confirmData: [],
            errorData: [],
            userData: userData,
            dataSource: []
        }
    }

    componentDidMount() {
        this.initData()
        this.loadData()
    }

    loadData() {
        if (!Util.isEmpty(this.props.insuSource)) {
            var insuSource = this.props.insuSource;
            for (var i = 0; i < insuSource.length; i++) {
                this.state.dataSource.push({
                    ordDesc: insuSource[i].defaultData.ordDesc,
                    insuMessage: insuSource[i].defaultData.insuMessage,
                    noINSUFlag: insuSource[i].defaultData.noINSUFlag,
                    medID: insuSource[i].defaultData.medID,
                })
            }
            this.setState({})
        }
    }

    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }

    initData() {
        this.columns = [
            {
                title: '医嘱名称',
                dataIndex: 'ordDesc',
                width: 180,
                align: 'center',
            }, {
                title: '确认信息',
                dataIndex: 'insuMessage',
                width: 120,
                align: 'center',
            }, {
                title: '是否医保',
                dataIndex: 'noINSUFlag',
                width: 80,
                align: 'center',
                render: (text, data, index) => {
                    return <Checkbox checked={data.noINSUFlag} onChange={this.handleChangeCheckbox.bind(this, 'noINSUFlag', data, index)} />
                }
            }
        ]
    }

    handleChangeCheckbox(name, data, index, e) {
        this.state.dataSource[index][name] = e.target.checked
        this.setState({})
    }

    closeModal() {
        let { onHide } = this.props;
        onHide && onHide(this.props.packageSource,this.state.dataSource);
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.setState({
            dataSource: []
        })
    }

    //提交
    onSubmit() {
        this.closeModal()
    }

    render() {
        return <div>
            <Modal className='confirm-advice-modal'
                title={'医保确认'}
                maskClosable={false}
                visible={this.props.isShow}
                okText={this.state.userData.language == 'EN' ? 'Submit' : '确定'}
                cancelText={this.state.userData.language == 'EN' ? 'Cancel' : '取消'}
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <div>
                    <Table columns={this.columns}
                        dataSource={this.state.dataSource}
                        bordered
                        pagination={false}></Table>
                </div>
            </Modal></div>
    }
}

export default Form.create()(ShowConfirmModal);